#!/usr/bin/perl
#create.cgi
#create the backups

require './backup-lib.pl';
&init_config();
&ReadParse();
#&header($text{'create_backup'}, "", "index", 0, 1,undef,undef);

if($in{'volselect'} eq "")
{
	&error($text{'SlectVolOrDie'});
}
if($in{'volselect'} ne &get_data_root())
{
	&change_vol($in{'volselect'});
}
if(($in{'user'} ne "") && ($in{'passwd'} ne ""))
{
	&save_passwd($in{'user'},$in{'passwd'});
}
my $newsche = "";
my @newschedulist = split(/\s+/, $in{'listinput'});
foreach $sche (@newschedulist)
{
	if($sche =~ /(\d+):(\d+)/)
	{
		$fraction = sprintf("%.8f",$2/60);
		$time=$1+$fraction;
		if($newsche eq "")
		{
			$newsche = $time;
		}
		else
		{
			$newsche = $newsche.",".$time;
		}	
	}
}
$configfile = $DATA_DIR."/conf/config.pl";
&read_file_byline($configfile,\@lines);
foreach $l (@lines)
{
        next if($l =~ /^#/);
        if($l =~ /Conf{WakeupSchedule}\s=\s\[(.*)\]/)
        {
		$l =~ s/Conf{WakeupSchedule} = \[.*\]/Conf{WakeupSchedule} = \[$newsche\]/;
                last;
        }
}
open(FILE,">$configfile")||die;
print FILE @lines;
close(FILE);

#&footer("/",$text{'index'});
&redirect("index.cgi?user=$in{'user'}");

sub change_vol()
{
	`/etc/init.d/backuppc stop`;
	my $vol=$_[0];
	`cp /etc/nas/dftcfg/backupdata/conf/hosts /usr/local/backuppc/data/conf/ -f`;
	`cp /etc/nas/dftcfg/backupdata/conf/config.pl /usr/local/backuppc/data/conf -f`;
	`mkdir /usrvol/$vol/backupdata/`;
	`chmod 777 /usrvol/$vol/backupdata/`;
	`echo "$vol">/etc/backuppc/DataVol`;
	`rm -f /usr/local/backuppc/data/pc/*`;
	`/etc/init.d/backuppc start`;
}
